﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class RequestRoom
    {
        public static RequestRoom FindByRequestIDRoomID_IncludeDeleted(linqDatabaseDataContext db, int RequestID, int RoomID)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RequestID == RequestID && u.RoomID == RoomID select u).SingleOrDefault());
        }

        public static RequestRoom FindByRequestIDRoomID(linqDatabaseDataContext db, int RequestID, int RoomID)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.RequestID == RequestID && u.RoomID == RoomID select u).SingleOrDefault());
        }

        public static List<RequestRoom> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<RequestRoom> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<RequestRoom> ToListByRequestID_IncludeDeleted(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RequestID == RequestID select u).ToList());
        }

        public static List<RequestRoom> ToListByRequestID(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RequestID == RequestID && !u.IsDeleted select u).ToList());
        }

        public static List<RequestRoom> ToListByRoomID_IncludeDeleted(linqDatabaseDataContext db, int RoomID)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RoomID == RoomID select u).ToList());
        }

        public static List<RequestRoom> ToListByRoomID(linqDatabaseDataContext db, int RoomID)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RoomID == RoomID && !u.IsDeleted select u).ToList());
        }

        public static List<RequestRoom> ToListByRoomID_IncludeDeleted_Filter(linqDatabaseDataContext db, int RoomID, DateTime From, DateTime To)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RoomID == RoomID && u.OfficialStartTimestamp < To && u.OfficialEndTimestamp > From select u).ToList());
        }

        public static List<RequestRoom> ToListByRoomID_IncludeDeleted_Filter(linqDatabaseDataContext db, int RoomID, int RequestRoomStatusID)
        {
            return ((from u in db.RequestRooms where u.RoomID == RoomID && u.RequestRoomStatusID == RequestRoomStatusID select u).ToList());
        }

        public static List<RequestRoom> ToListByRoomID_Filter(linqDatabaseDataContext db, int RoomID, DateTime From, DateTime To)
        {
            var subquery = (from u in db.RequestRooms where u.ID == (from v in db.RequestRooms where v.RequestID == u.RequestID && v.RoomID == u.RoomID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.RoomID == RoomID && u.OfficialStartTimestamp < To && u.OfficialEndTimestamp > From && !u.IsDeleted select u).ToList());
        }

        public static List<RequestRoom> HistoryToListByRequestIDRoomID(linqDatabaseDataContext db, int RequestID, int RoomID)
        {
            return ((from u in db.RequestRooms where u.RequestID == RequestID && u.RoomID == RoomID select u).ToList());
        }
    }
}
